<template>
  <div class="login-container">
    <el-card class="login-card">
      <h1 class="login-title">系统注册</h1>
      <div class="login-form">
        <el-form :model="registerForm" ref="registerFormRef" :rules="rules" @submit.prevent="handleRegister"
                 status-icon>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="username">
                <el-input v-model="registerForm.username" autocomplete="off" prefix-icon="user" maxlength="10"
                          placeholder="请输入用户名"
                          show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item prop="password">
                <el-input type="password" v-model="registerForm.password" autocomplete="off" prefix-icon="lock"
                          placeholder="请输入密码"
                          show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item prop="rePassword">
                <el-input type="password" v-model="registerForm.rePassword" autocomplete="off" prefix-icon="lock"
                          placeholder="请再次输入密码"
                          show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item prop="checked1">
            <el-checkbox v-model="registerForm.checked1" label="我同意此协议" size="large"/>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleRegister(registerFormRef)" style="width: 100%;">注册</el-button>
          </el-form-item>

        </el-form>

      </div>

      <!-- 无帐号，点此注册 -->
      <el-row style="text-align: right;">
        <el-col>
          <el-text class="w-150px mb-2">
            若无帐号，点此>>
            <RouterLink to="/login" style="font-size: 1.2em;">登录</RouterLink>
          </el-text>
        </el-col>
      </el-row>
    </el-card>
  </div>

</template>

<script setup>
import {ref, reactive} from 'vue';
import {RouterLink, useRouter} from 'vue-router'

const registerForm = reactive({
  username: '',//用户名
  password: '',//密码
  rePassword: '',//去人密码
  checked1: null
});

//验证信息
const registerFormRef = ref()

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码!'))
  } else {
    if (registerForm.rePassword !== '') {
      if (!registerFormRef.value) return
      registerFormRef.value.validateField('rePassword')
    }
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码!'))
  } else if (value !== registerForm.password) {
    callback(new Error("两次输入不一致!"))
  } else {
    callback()
  }
}

const rules = reactive({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 4, max: 10, message: '长度必须是 4 到 10', trigger: 'blur'},
  ],
  password: [{validator: validatePass, trigger: 'blur'}],
  rePassword: [{validator: validatePass2, trigger: 'blur'}],
  checked1: [{
    type: 'array',
    required: true,
    message: '请勾选协议',
    trigger: 'change',
  },],
})

const router = useRouter()

function handleRegister(formEl) {
  console.log('注册信息:', registerForm);

  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      //todo 这里可以添加注册逻辑

      //跳转到login页面
      router.push("/login")
    } else {
      console.log('error submit!')
    }
  })


}

</script>
<style scoped>

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/loginbg-1.png'); /* 假设背景图片在assets目录下 */
  background-size: cover;
  background-position: center;
}

.login-card {
  width: 420px;
  padding: 20px;
  /* background-color: #fafafa; */
  background-color: #fefefe;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.login-form {
  width: 100%;
}

.captcha-image {
  cursor: pointer; /* 当鼠标悬停时显示为可点击状态 */
  margin-left: 10px; /* 与输入框保持一定间距 */
  height: 36px; /* 控制图片高度，根据实际情况调整 */
  vertical-align: middle; /* 垂直居中对齐 */
}
</style>